package nus.mtech.presentation;


import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import nus.mtech.constant.PresentationConstant;
import nus.mtech.user.Administrator;
import nus.mtech.user.User;
import nus.mtech.util.DateUtil;
import nus.mtech.util.ExpDataUtil;
import nus.mtech.util.ImpDataUtil;
import nus.mtech.util.LoadConfig;
import nus.mtech.customer.*;

public class Console extends JPanel{
    
    private static JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    private static JTabbedPane customersTabbedPane = new JTabbedPane(JTabbedPane.TOP);
    private static JTabbedPane subscriptionTabbedPane = new JTabbedPane(JTabbedPane.TOP);
//    private static JScrollPane importTabPanel;
    private static JScrollPane loginTabPanel;
    private static JScrollPane logoutTabPanel;
    private static JScrollPane customerTabPanel;
    private static JScrollPane complaintTabPanel;
    private static JScrollPane digitalTabPanel;
    private static JScrollPane mobileTabPanel;
    private static JScrollPane cableTabPanel;
    private static JScrollPane billingTabPanel;
//    private static JScrollPane exportTabPanel;
//    private ImportPanel setImportTabPanel;
    private LoginPanel setLoginTabPanel;
    private CustomerPanel setCustomerPanel;
    static ComplaintPanel setComplaintPanel;
    private DigitalPanel setDigitalPanel;
    private MobilePanel setMobilePanel;
    private CableTVPanel setCableTVPanel;
    private ViewBillPanel setBillingPanel;
//    private ExportPanel setExportPanel;
    private static String companyAddress = "";
    private static User loginUser;

    private Console() throws Exception {
        super();
       
        JFrame frame = new JFrame("Billing Application (Ver 0.0.1)");
       

        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        initComponents(frame);
        ImpDataUtil.LoadData();

        frame.setSize(PresentationConstant.CONSOLE_WIDTH,
                PresentationConstant.CONSOLE_HEIGHT);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int response = JOptionPane.showConfirmDialog(null,
                        "All data will be cleared on closing the application. Do you want to proceed?",
                        "Exit Application?", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE);
                if (response == JOptionPane.YES_OPTION) {
                	try{
                		ExpDataUtil.saveData();
                	}catch(Exception ex){
                		ex.printStackTrace();
                	}
                    System.exit(0);
                }
                else
                    return;
            }
        });
    }

    private void initComponents(JFrame frame) {
        try {
        	setLoginTabPanel = new LoginPanel();
        	loginTabPanel = new JScrollPane(setLoginTabPanel);
//            setImportTabPanel = new ImportPanel();
//            importTabPanel = new JScrollPane(setImportTabPanel);
//            tabbedPane.addTab("Import Data", importTabPanel);
            
            //Customers and Complain Sub Panels
            setCustomerPanel = CustomerPanel.getInstance();
            customerTabPanel = new JScrollPane(setCustomerPanel);
            customersTabbedPane.addTab("Customer Information", customerTabPanel);
            setComplaintPanel = new ComplaintPanel();
            complaintTabPanel = new JScrollPane(setComplaintPanel);
            customersTabbedPane.addTab("Complaint Information", complaintTabPanel);
            //customersTabbedPane.addChangeListener(this);
            
            //Subscription Panels
            setDigitalPanel = new DigitalPanel();
            digitalTabPanel = new JScrollPane(setDigitalPanel);
            subscriptionTabbedPane.addTab("Digital Voice", digitalTabPanel);
            setMobilePanel = new MobilePanel();
            mobileTabPanel = new JScrollPane(setMobilePanel);
            subscriptionTabbedPane.addTab("Mobile Voice", mobileTabPanel);
            setCableTVPanel = new CableTVPanel();
            cableTabPanel = new JScrollPane(setCableTVPanel);
            subscriptionTabbedPane.addTab("Cable TV", cableTabPanel);
            
            //Bill Panels
            setBillingPanel = ViewBillPanel.getInstance();
            billingTabPanel = new JScrollPane(setBillingPanel);
            
//            setExportPanel = new ExportPanel();
//            exportTabPanel = new JScrollPane(setExportPanel);
//            tabbedPane.addTab("Export Data", exportTabPanel);
            
            setLayout(new java.awt.GridLayout(1, 0, 0, 0));
            frame.add(tabbedPane);
            tabbedPane.setVisible(true);
            
            initLoginComponent();
//            enableAllPanes(false);
        }
        catch (Exception e) {
            System.out.println("initComponents:" + e.toString());
        }
    }
    
    public static void initLoginComponent(){
    	tabbedPane.removeAll();
    	tabbedPane.addTab("Login", loginTabPanel);
    }
    
    private static void initMainComponent(){
    	tabbedPane.removeAll();
    	
    	//Logout Panel
    	LogoutPanel setLogoutTabPanel = LogoutPanel.getInstance();
    	String userId = loginUser.getUserID();
    	JLabel lblWelcome = setLogoutTabPanel.getLblWelcome();
    	lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
    	lblWelcome.setFont(new Font("", Font.BOLD, 20));
    	lblWelcome.setText("Welcome " + userId);
    	lblWelcome.setBounds(0, 200, 1004, 21);
        logoutTabPanel = new JScrollPane(setLogoutTabPanel);
    	tabbedPane.addTab("Login", logoutTabPanel);
    	tabbedPane.addTab("Customer Information", new JPanel().add(customersTabbedPane));
    	tabbedPane.addTab("Subscriptions", new JPanel().add(subscriptionTabbedPane));
    	tabbedPane.addTab("Bills", billingTabPanel);
    }
    
    public static void enableAllPanes(boolean enabled) {
    	initMainComponent();
    	
    	int tabCount = tabbedPane.getTabCount();
        for (int i = 1; i < tabCount; i++) {
        	togglePaneEnabler(i, enabled);
        }
    }
    
    /**
     * Static method to set the enabled status for pane
     * 
     * @param int - i
     * @param boolean - enabled
     */
    public static void togglePaneEnabler(int i, boolean enabled) {
        tabbedPane.setEnabledAt(i, enabled);
    }
    
    public static void main(String[] args) {
    	try{
    		new Console();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    
    /**
     * Static method to toggle selected pane
     * 
     * @param int - i
     */
    public static void toggleCustomerSelectedPane(int i) {
    	customersTabbedPane.setSelectedIndex(i);
    }

    

    
    public static void setCompanyAdd(String add) {
    	companyAddress = add;
    }

	/**
	 * Get the loginUser
	 * 
	 * @return the loginUser
	 */
	public static User getLoginUser() {
		return loginUser;
	}

	/**
	 * Set the loginUser
	 * 
	 * @param loginUser the loginUser to set
	 */
	public static void setLoginUser(User loginUser) {
		Console.loginUser = loginUser;
	}
	
	//public void stateChanged(ChangeEvent evt) {
//		if (customersTabbedPane.getSelectedIndex() == 1) {
//			ComplaintManager compMgr = ComplaintManager
//					.getComplaintManagerInstance();
//			ArrayList<Complaint> compArray = new ArrayList<Complaint>();
//			String accountNo = "";
//			if (setCustomerPanel.getAccountNo() != "") {
//				accountNo = CustomerPanel.getInstance().getAccountNo();
//				compArray = compMgr.getComplaintByAccountNo(accountNo);
//
////				System.out.println(setComplaintPanel.comboBoxDateTime
////						.getItemCount());
//
//				while (setComplaintPanel.comboBoxDateTime.getItemCount() > 1) {
//					setComplaintPanel.comboBoxDateTime.removeItemAt(1);
//				}
//
//				if (compArray != null) {
//					for (Complaint comp : compArray) {
//						setComplaintPanel.comboBoxDateTime.addItem(DateUtil
//								.formatDate(comp.getComplaintDate(),
//										DateUtil.DATE_TIME_FORMAT));
//					}
//				}
//			}
//		}
	//}
}
